
package com.sskj.jkzx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.basic.BasicAN;
import com.sskj.jkzx.domain.entity.basic.BasicKV;
import com.sskj.jkzx.domain.entity.quantity.HtEngQuantity;
import com.sskj.jkzx.domain.entity.quantity.HtQuantity;
import com.sskj.jkzx.domain.entity.quantity.SgxkEngQuantity;
import com.sskj.jkzx.domain.entity.quantity.SgxkQuantity;
import com.sskj.jkzx.domain.entity.quantity.TsEngQuantity;
import com.sskj.jkzx.domain.entity.quantity.TsQuantity;
import com.sskj.jkzx.domain.entity.quantity.WgclQuantity;
import com.sskj.jkzx.domain.entity.quantity.WgclTypeQuantity;
import com.sskj.jkzx.domain.entity.quantity.ZtbEngQuantity;
import com.sskj.jkzx.domain.entity.quantity.ZtbQuantity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "**/sskj/xmzx", method = { RequestMethod.POST, RequestMethod.GET })
@Api(value = "项目中心信息查询", description = "项目中心信息查询")
public class XmzxInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(XmzxInfoServiceImpl.class);

	@Autowired
	QuantityServiceImpl quantityServiceImpl;

	/***
	 * 项目工程数量统计信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/eq", method = RequestMethod.GET)
	@ApiOperation(value = "项目工程数量统计信息查询", httpMethod = "GET")
	public Map<String, BasicAN> queryEngQuantity() {

		Map<String, BasicAN> result = new HashMap<String, BasicAN>();

		TsQuantity ts = quantityServiceImpl.tseqt();

		HtQuantity ht = quantityServiceImpl.hteqt();

		SgxkQuantity sgxk = quantityServiceImpl.sgxkeqt();

		ZtbQuantity ztb = quantityServiceImpl.ztbeqt();

		WgclQuantity wgcl = quantityServiceImpl.wgcleqt();

		if (ts != null) {
			result.put("ts", new BasicAN(Long.valueOf(ts.getTotal()), Long.valueOf(ts.getNewly())));
		} else {
			result.put("ts", new BasicAN(0, 0));
		}

		if (ztb != null) {
			result.put("ztb", new BasicAN(Long.valueOf(ztb.getTotal()), Long.valueOf(ztb.getNewly())));
		} else {
			result.put("ztb", new BasicAN(0, 0));
		}

		if (ht != null) {
			result.put("sght", new BasicAN(Long.valueOf(ht.getTotal()), Long.valueOf(ht.getNewly())));
		} else {
			result.put("sght", new BasicAN(0, 0));
		}

		if (sgxk != null) {
			result.put("xkz", new BasicAN(Long.valueOf(sgxk.getTotal()), Long.valueOf(sgxk.getNewly())));
		} else {
			result.put("xkz", new BasicAN(0, 0));
		}

		if (wgcl != null) {
			result.put("wg", new BasicAN(Long.valueOf(wgcl.getTotal()), Long.valueOf(wgcl.getNewly())));
		} else {
			result.put("wg", new BasicAN(0, 0));
		}

		logger.info(JSONObject.toJSONString(result));
		return result;
	}

	/***
	 * 图审基础数据
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/ts", method = RequestMethod.GET)
	@ApiOperation(value = "图审基础数据", httpMethod = "GET")
	public Map<String, String> queryTs() {

		Map<String, String> result = new HashMap<String, String>();

		TsEngQuantity tsengqt = quantityServiceImpl.tsengqt();

		if (tsengqt != null) {
			result.put("clz", String.format("%0" + 4 + "d", Integer.parseInt(tsengqt.getSysl())));
			result.put("ywc", String.format("%0" + 4 + "d", Integer.parseInt(tsengqt.getSywc())));
		} else {
			// 在建工程
			result.put("clz", "0000");
			// 竣工工程
			result.put("ywc", "0000");
		}

		logger.info(JSONObject.toJSONString(result));
		return result;
	}

	/***
	 * 招投标数据
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/ztb", method = RequestMethod.GET)
	@ApiOperation(value = "招投标数据", httpMethod = "GET")
	public Map<String, List<BasicKV>> queryZtb() {

		Map<String, List<BasicKV>> result = new HashMap<String, List<BasicKV>>();

		List<BasicKV> category = new ArrayList<BasicKV>();
		List<BasicKV> jg = new ArrayList<BasicKV>();
		List<BasicKV> zj = new ArrayList<BasicKV>();

		category.add(new BasicKV("1", "公开招标"));
		category.add(new BasicKV("2", "邀请招标"));
		category.add(new BasicKV("3", "直接发包"));
		category.add(new BasicKV("4", "异地招标"));
		result.put("category", category);

		ZtbEngQuantity ztbjgeqt = quantityServiceImpl.ztbjgengqt();
		ZtbEngQuantity ztbzjeqt = quantityServiceImpl.ztbzjengqt();

		jg.add(new BasicKV("公开招标", ztbjgeqt == null ? "0" : ztbjgeqt.getGkzb()));
		jg.add(new BasicKV("邀请招标", ztbjgeqt == null ? "0" : ztbjgeqt.getYqzb()));
		jg.add(new BasicKV("直接发包", ztbjgeqt == null ? "0" : ztbjgeqt.getZjfb()));
		jg.add(new BasicKV("异地招标", ztbjgeqt == null ? "0" : ztbjgeqt.getYdzb()));

		zj.add(new BasicKV("公开招标", ztbzjeqt == null ? "0" : ztbzjeqt.getGkzb()));
		zj.add(new BasicKV("邀请招标", ztbzjeqt == null ? "0" : ztbzjeqt.getYqzb()));
		zj.add(new BasicKV("直接发包", ztbzjeqt == null ? "0" : ztbzjeqt.getZjfb()));
		zj.add(new BasicKV("异地招标", ztbzjeqt == null ? "0" : ztbzjeqt.getYdzb()));

		result.put("zj", zj);
		result.put("jg", jg);

		logger.info(JSONObject.toJSONString(result));
		return result;
	}

	/***
	 * 施工合同数据
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/sght", method = RequestMethod.GET)
	@ApiOperation(value = "施工合同数据", httpMethod = "GET")
	public Map<String, List<BasicKV>> querySght() {

		Map<String, List<BasicKV>> result = new HashMap<String, List<BasicKV>>();

		HtEngQuantity hteqt = quantityServiceImpl.htengqt();

		List<BasicKV> category = new ArrayList<BasicKV>();
		List<BasicKV> js = new ArrayList<BasicKV>();
		List<BasicKV> wjs = new ArrayList<BasicKV>();
		List<BasicKV> sy = new ArrayList<BasicKV>();

		category.add(new BasicKV("1", "施工"));
		category.add(new BasicKV("2", "劳务分包"));

		if (hteqt != null) {
			js.add(new BasicKV("施工", hteqt.getSgjs()));
			js.add(new BasicKV("劳务分包", hteqt.getLwfbjs()));
			wjs.add(new BasicKV("施工", hteqt.getSgwjs()));
			wjs.add(new BasicKV("劳务分包", hteqt.getLwfbwjs()));
			sy.add(new BasicKV("施工", hteqt.getSg()));
			sy.add(new BasicKV("劳务分包", hteqt.getLwfb()));

		} else {
			js.add(new BasicKV("施工", "0"));
			js.add(new BasicKV("劳务分包", "0"));
			wjs.add(new BasicKV("施工", "0"));
			wjs.add(new BasicKV("劳务分包", "0"));
			sy.add(new BasicKV("施工", "0"));
			sy.add(new BasicKV("劳务分包", "0"));
		}
		result.put("sy", sy);
		result.put("js", js);
		result.put("wjs", wjs);
		result.put("category", category);

		logger.info(JSONObject.toJSONString(result));
		return result;
	}

	/***
	 * 违规处理数据分类
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/wg", method = RequestMethod.GET)
	@ApiOperation(value = "违规处理数据分类", httpMethod = "GET")
	public Map<String, List<BasicKV>> queryWg() {

		Map<String, List<BasicKV>> result = new HashMap<String, List<BasicKV>>();
		List<BasicKV> category = new ArrayList<BasicKV>();
		List<BasicKV> jg = new ArrayList<BasicKV>();
		List<BasicKV> zj = new ArrayList<BasicKV>();

		category.add(new BasicKV("1", "违法强条"));
		category.add(new BasicKV("2", "停工通知"));
		category.add(new BasicKV("3", "整改通知"));
		category.add(new BasicKV("4", "不合格报告"));
		category.add(new BasicKV("5", "无证施工"));
		category.add(new BasicKV("6", "市场行为查处"));
		category.add(new BasicKV("7", "不良行为"));

		// 全年
		WgclTypeQuantity zjeqt = quantityServiceImpl.wgclzjtypeeqt();
		zj.add(new BasicKV("停工通知", zjeqt == null ? "0" : zjeqt.getTgtz()));
		zj.add(new BasicKV("整改通知", zjeqt == null ? "0" : zjeqt.getZgtz()));
		// zj.add(new BasicKV("不合格报告", zjeqt == null ? "0" : zjeqt.getBhgbg()));
		zj.add(new BasicKV("无证施工", zjeqt == null ? "0" : zjeqt.getWzsg()));
		// zj.add(new BasicKV("市场查处", zjeqt == null ? "0" : zjeqt.getScxwcc()));
		zj.add(new BasicKV("不良行为", zjeqt == null ? "0" : zjeqt.getBlxw()));

		// 本月新增
		WgclTypeQuantity jgeqt = quantityServiceImpl.wgcljgtypeeqt();
		jg.add(new BasicKV("停工通知", jgeqt == null ? "0" : jgeqt.getTgtz()));
		jg.add(new BasicKV("整改通知", jgeqt == null ? "0" : jgeqt.getZgtz()));
		// jg.add(new BasicKV("不合格报告", jgeqt == null ? "0" : jgeqt.getBhgbg()));
		jg.add(new BasicKV("无证施工", jgeqt == null ? "0" : jgeqt.getWzsg()));
		// jg.add(new BasicKV("市场查处", jgeqt == null ? "0" : jgeqt.getScxwcc()));
		jg.add(new BasicKV("不良行为", jgeqt == null ? "0" : jgeqt.getBlxw()));

		result.put("category", category);
		result.put("zj", zj);
		result.put("jg", jg);
		logger.info(JSONObject.toJSONString(result));

		return result;
	}

	/***
	 * 施工许可分类数据
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/xkz", method = RequestMethod.GET)
	@ApiOperation(value = "施工许可分类数据", httpMethod = "GET")
	public Map<String, List<BasicKV>> queryXkz() {

		Map<String, List<BasicKV>> result = new HashMap<String, List<BasicKV>>();
		List<BasicKV> category = new ArrayList<BasicKV>();

		SgxkEngQuantity jgeqt = quantityServiceImpl.sgxkjgengqt();
		SgxkEngQuantity zjeqt = quantityServiceImpl.sgxkzjengqt();

		category.add(new BasicKV("1", "房建"));
		category.add(new BasicKV("2", "市政"));
		category.add(new BasicKV("3", "园林"));
		category.add(new BasicKV("4", "装饰"));
		category.add(new BasicKV("5", "其他"));

		// 全部施工许可
		List<BasicKV> total = new ArrayList<BasicKV>();

		// 本月新增
		List<BasicKV> add = new ArrayList<BasicKV>();

		total.add(new BasicKV("房建", zjeqt == null ? "0" : zjeqt.getFj()));
		total.add(new BasicKV("市政", zjeqt == null ? "0" : zjeqt.getSz()));
		total.add(new BasicKV("园林", zjeqt == null ? "0" : zjeqt.getYllh()));
		total.add(new BasicKV("装饰", zjeqt == null ? "0" : zjeqt.getZszx()));
		total.add(new BasicKV("其他", zjeqt == null ? "0" : zjeqt.getQt()));

		add.add(new BasicKV("房建", jgeqt == null ? "0" : jgeqt.getFj()));
		add.add(new BasicKV("市政", jgeqt == null ? "0" : jgeqt.getSz()));
		add.add(new BasicKV("园林", jgeqt == null ? "0" : jgeqt.getYllh()));
		add.add(new BasicKV("装饰", jgeqt == null ? "0" : jgeqt.getZszx()));
		add.add(new BasicKV("其他", jgeqt == null ? "0" : jgeqt.getQt()));

		result.put("category", category);
		result.put("zj", total);
		result.put("jg", add);

		logger.info(JSONObject.toJSONString(result));
		return result;
	}

}
